package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DatabaseOperationResult;
import com.xyoto.community.constant.OssBucketModuleName;
import com.xyoto.community.dto.AddRotationChartDTO;
import com.xyoto.community.dto.UpdateRotationChartDTO;
import com.xyoto.community.entity.main.RotationChart;
import com.xyoto.community.enums.CommonEnum;
import com.xyoto.community.enums.FileEnum;
import com.xyoto.community.enums.RotationChartEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.RotationChartMapper;
import com.xyoto.community.service.RotationChartService;
import com.xyoto.community.utils.Result;
import com.xyoto.community.utils.XYOtoOss;
import com.xyoto.community.vo.RotationChartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author coderFu
 * @create 2021/11/17
 */
@Service
@Slf4j
public class RotationChartServiceImpl implements RotationChartService {

    @Autowired
    private RotationChartMapper rotationChartMapper;

    @Resource
    private XYOtoOss xyOtoOss;


    @Override
    public Result addRotationChart(AddRotationChartDTO addRotationChartDTO) {
        //处理文件
        MultipartFile file = addRotationChartDTO.getFile();
        if (file == null) {
            ExceptionUtils.error(FileEnum.FILE_NOT_EXIST);
        }
        RotationChart rotationChart = new RotationChart();
        //上传文件
        try {
            String path = xyOtoOss.uploadFileAvatar(file, OssBucketModuleName.HOME_ROTATION_CHART_PICTURE);
            rotationChart.setPath(path);
        } catch (IOException e) {
            e.printStackTrace();
            ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
        }
        //处理顺序问题
        //查询最大的轮播图id
        Integer maxId = rotationChartMapper.getMaxRotationChartId();
        if (maxId == null){
            maxId = 0;
        }
        Integer orders = addRotationChartDTO.getOrders();
        if (orders == null || orders > maxId) {
            orders = maxId + 1;
        } else {
            //查询要交换对象的信息
            RotationChart rotationChart1 = rotationChartMapper.getRotationChartByOrders(orders);
            if (rotationChart1 == null) {
                log.info("不存在另外的轮播图哦");
            } else {
                orders = rotationChart1.getOrders();
                rotationChart1.setOrders(maxId + 1);
                //更新旧的轮播图信息
                Integer integer = rotationChartMapper.updateRotationChartById(rotationChart1);
                if (integer < DatabaseOperationResult.SUCCESS) {
                    ExceptionUtils.error(RotationChartEnum.UPDATE_ROTATION_CHART_FAIL);
                }
            }
        }
        rotationChart.setOrders(orders);
        rotationChart.setLink(addRotationChartDTO.getLink());
        rotationChart.setStatus(addRotationChartDTO.getStatus());
        Integer integer = rotationChartMapper.addRotationChart(rotationChart);
        if (integer < DatabaseOperationResult.SUCCESS) {
            ExceptionUtils.error(RotationChartEnum.ADD_ROTATION_CHART_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateRotationChart(UpdateRotationChartDTO updateRotationChartDTO) {
        RotationChart newRotationChart = new RotationChart();
        //判断是否有新的图片上传
        MultipartFile file = updateRotationChartDTO.getFile();
        //查询轮播图详细信息
        RotationChart rotationChart = rotationChartMapper.getRotationChartById(updateRotationChartDTO.getId());
        String sourceImgPath = rotationChart.getPath();
        if (file != null) {
            //上传图片文件
            try {
                String path = xyOtoOss.uploadFileAvatar(file, OssBucketModuleName.HOME_ROTATION_CHART_PICTURE);
                newRotationChart.setPath(path);
            } catch (IOException e) {
                e.printStackTrace();
                ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
            }
        }
        //查询最大的轮播图id
        Integer maxId = rotationChartMapper.getMaxRotationChartId();
        Integer orders = updateRotationChartDTO.getOrders();
        if (!rotationChart.getOrders().equals(orders)) {
            if (orders > maxId) {
                orders = maxId;
            }
            //查询要替换的轮播图情况
            RotationChart rotationChartChange = rotationChartMapper.getRotationChartByOrders(orders);
            if (rotationChartChange == null) {
                log.info("原来没有轮播图哦");
            } else {
                rotationChartChange.setOrders(orders);
                Integer integer = rotationChartMapper.updateRotationChartById(rotationChartChange);
                if (integer < 1) {
                    ExceptionUtils.error(RotationChartEnum.UPDATE_ROTATION_CHART_FAIL);
                }
            }
        }
        newRotationChart.setOrders(orders);
        newRotationChart.setStatus(updateRotationChartDTO.getStatus());
        newRotationChart.setLink(updateRotationChartDTO.getLink());
        newRotationChart.setId(updateRotationChartDTO.getId());
        Integer integer = rotationChartMapper.updateRotationChart(newRotationChart);
        if (integer < DatabaseOperationResult.SUCCESS) {
            ExceptionUtils.error(RotationChartEnum.UPDATE_ROTATION_CHART_FAIL);
        }
        if (file != null) {
            //删除原来的轮播图图片
            xyOtoOss.deleteFileAvatar(sourceImgPath);
            log.info("原图片删除成功");
        }
        return Result.success();
    }

    @Override
    public Result getRotationChartList() {
        List<RotationChartVO> rotationChartVoS = rotationChartMapper.getRotationChartList();
        return Result.success(rotationChartVoS);
    }

    @Override
    public Result getRotationChartDetailsById(Integer id) {
        if (id == null || id <0){
            ExceptionUtils.error(CommonEnum.REQUEST_PARAM_ERROR);
        }
        RotationChart rotationChartById = rotationChartMapper.getRotationChartById(id);
        return Result.success(rotationChartById);
    }
}
